<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec" %>
<!DOCTYPE html>
<%@include file="include-head.jsp"%>
<body>
<%@include file="include-nav.jsp"%>
<%@include file="include-sidebar.jsp"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="include-modal-message-edit.jsp"%>

<link rel="stylesheet" type="text/css" href="/static/css/pagination01.css"/>
<div class="container-fluid">
    <div class="row">

        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr >
                                <th width="30">#</th>
                                <th>消息描述</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                           <%-- <tbody>
                         <c:forEach var="list" items="${requestScope['PAGE-INFO'].list}">
                             <tr>
                                 <td>${list.id}</td>
                                 <td>${list.content}</td>
                                 <td>
                                     <button type="button" class="btn btn-primary btn-xs"><i class="glyphicon glyphicon-pencil"></i></button>
                                 </td>
                             </tr>
                         </c:forEach>
                            </tbody>--%>

                            <tbody id="messageTableBody">

                            <%--ajax加载数据--%>

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

</body>
</html>

<script type="text/javascript" src="script/jquery.pagination.js"></script>
<%--<script type="text/javascript" src="script/my-message.js"></script>--%>
<script>

    //定义全局变量
    $(function () {
        //定义全局变量
        window.pageSize = 5;
        window.pageNum = 1;
        window.keyword = "";

        window.messageIdArray=new Array();
        //显示数据
        showPage();
    });

    function showPage() {
        $.ajax({
            url: "/message/search/by/keyword",
            type: "post",
            data: {
                pageNum: window.pageNum,
                pageSize: window.pageSize,
                keyword: window.keyword
            },
            dataType: "json",
            success: function (result) {
                if (result.result == "SUCCESS") {
                    //解析数据
                    $("#messageTableBody").empty();//empty 移除里面的所有内容
                    //获取数据集合
                    var list = result.data.list;
                    if (list.length > 0) {
                        //有数据
                        //var list=list[i];
                        for (var i = 0; i < list.length; i++) {
                            var $tr = $('<tr></tr>');
                            var $td1 = $('<td>' + (i + 1) + '</td>');
                            var $td3 = $('<td>' + list[i].content + '</td>');

                            var $td4 = $('<td></td>');

                            $td4.append('<button  messageId="'+list[i].id+'"   type="button" class="btn btn-primary btn-xs editBtn"><i class=" glyphicon glyphicon-pencil"></i><tton>');

                            $tr.append($td1);
                            $tr.append($td3);
                            $tr.append($td4);

                            $("#messageTableBody").append($tr);
                        }
                    } else {
                        //没数据
                        var $tr = $('<tr></tr>');
                        var $td = $('<td colspan="4">对不起，没有查询到相应的角色！！</td>');
                        $tr.append($td);
                        $("#messageTableBody").append($tr);
                    }
                } else {
                    layer.msg(result.message + "获取角色数据失败!");
                }
            }
        });
    }


    //修改
    $("#messageTableBody").on("click",".editBtn",function(){
        // 1.获取当前按钮的messageId
        window.messageId = $(this).attr("messageId");
        // 2.获取当前按钮所在行的messageName
        var messageName = $(this).parents("tr").children("td:eq(1)").text();
        // 3.修改模态框中文本框的value值，目的是在显示messageName
        $("#messages").val(messageName);
        // 4.打开模态框
        $("#editModal").modal("show");
    });
    //确认修改
    $("#editModalBtn").click(function () {
        // 1.获取文本框值
        var messageName = $.trim($("#messages").val());
        if(messageName == null || messageName == "") {
            layer.msg("请输入有效消息描述！");
            return ;
        }
        $.ajax({
            url:"/message/update/role",
            type:"post",
            data:{
                "id":window.messageId,
                "content":messageName
            },
            dataType:"json",
            success:function (response) {
                var result = response.result;
                if(result == "SUCCESS") {
                    layer.msg("操作成功！");
                    // 3.操作成功重新分页
                    showPage();
                }
                if(result == "FAILED") {
                    layer.msg(response.message);
                }
                // 4.不管成功还是失败，关闭模态框
                $("#editModal").modal("hide");
            }
        });
    });

</script>
